<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 快照沙箱
    class SnapshotSandbox {
      constructor () {
        this.proxy = window
        this.modifyPropsMap= {} // 记录在window上的修改
        this.active()
      }
      active () {
        this.windowSnapshot = {} // 拍照
        for (const prop in window) {
          if (window.hasOwnProperty(prop)) {
            this.windowSnapshot[prop] = window[prop]
          }
        }
        Object.keys(this.modifyPropsMap).forEach(p => {
          window[p] = this.modifyPropsMap[p]
        })
      }
      inactive () {
        for (const prop in window) {
          if (window.hasOwnProperty(prop)) {
            if (window[prop] === this.windowSnapshot[prop]) {
              this.modifyPropsMap[prop] = window[prop]
              window[prop] = this.windowSnapshot[prop]
            }
          }
        }
      }
    }
    let sandbox = new SnapshotSandbox()
    ((window) => {
      debugger
      window.a = 1
      window.b = 1
      console.log(window.a, window.b)
      sandbox.inactive()
      console.log(window.a, window.b)
      sandbox.active()
      console.log(window.a, window.b)
    })(sandbox.proxy)

    // 如果是多个应用就不能使用这种方式， es6 的 proxy 可以实现
    // 2. 代理沙箱可以实现多应用沙箱。把不同应用用不同的代理来完成
  </script>
</body>
</html>